#Twisted Shootout
#Copyright (C) 2007  Han Dao
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

#You can contact the author at wikipediankiba@gmail.com
import pygame
import maplaw

class Maposition:
	def __init__(self,engine,):
		self.engine = engine
		self.move = self.engine.move
		self.height = self.engine.height
		self.mapobj = self.engine.mapobj
		self.i = self.engine.items
	def set(self,p,e,w):
		self.p = self.engine.p
		self.e = self.engine.e
		self.w = self.engine.w
		self.maplaw = maplaw.MapLaw(self)
	def loadcollide(self,collide):
		self.maplaw.load(collide)
	def compute(self):
		self.maplaw.compute()
		self.backcompute()
		self.forwardcompute()
		self.downcompute()
		self.upcompute()
	def backcompute(self):
		if self.p.rect.x < 250:
			move = 250 - self.p.rect.x
			self.move = self.move + move
			if self.leftlimit() == True:
				return
			self.xsynchronize(move)
	def forwardcompute(self):
		if self.p.rect.x > 350:
			move = self.p.rect.x - 350
			self.move = self.move - move
			if self.rightlimit() == True:
				return
			move = -move
			self.xsynchronize(move)
	def downcompute(self):
		if self.p.rect.y > 530:
			move = self.p.rect.y - 530
			self.height = self.height - move
			if self.downlimit() == True:
				return
			move = -move
			self.ysynchronize(move)
	def upcompute(self):
		if self.p.rect.y < 60:
			move = 60 - self.p.rect.y
			self.height = self.height + move
			if self.uplimit() == True:
				return
			self.ysynchronize(move)
	def ysynchronize(self,move):
		for v in self.mapobj:
			v.rect.y = v.rect.y + move
		for i in self.i.items:
			i.rect.y = i.rect.y + move
		if self.engine.poscompute == True:
			for e in self.e.e:
				e.rect.y = e.rect.y + move
			for w in self.w.w:
				w.rect.y + w.rect.y + move
			self.p.rect.y = self.p.rect.y + move
	def xsynchronize(self,move):
		for v in self.mapobj:
			v.rect.x = v.rect.x + move
		for i in self.i.items:
			i.rect.x = i.rect.x + move
		if self.engine.poscompute == True:
			for e in self.e.e:
				e.rect.x = e.rect.x + move
			for w in self.w.w:
				w.rect.x + w.rect.x + move
			self.p.rect.x = self.p.rect.x + move
	def moveleft(self):
		move = 80
		self.move = self.move + move
		self.xsynchronize(move)
		self.leftlimit()
	def moveright(self):
		move = -80
		self.move = self.move + move
		self.xsynchronize(move)
		self.rightlimit()
	def movedown(self):
		move = -60
		self.height = self.height + move
		self.ysynchronize(move)
		self.downlimit()
	def moveup(self):
		move = 60
		self.height = self.height + move
		self.ysynchronize(move)
		self.uplimit()
	def uplimit(self):
		if self.height > 0:
			self.height = 0
			s = self.mapobj[0].rect.y
			self.limitactiony(s)
			return True
		return False
	def downlimit(self):
		if self.height < -1200:
			self.height = -1200
			s = self.mapobj[0].rect.y + 1200
			self.limitactiony(s)
			return True
		return False
	def rightlimit(self):
		if self.move < -1600:
			self.move = -1600
			s = self.mapobj[0].rect.x + 1600
			self.limitactionx(s)
			return True
		return False
	def leftlimit(self):
		if self.move > 0:
			self.move = 0
			s = self.mapobj[0].rect.x
			self.limitactionx(s)
			return True
		return False
	def limitactionx(self,s):
		for m in self.mapobj:
			m.rect.x = m.rect.x - s
		for i in self.i.items:
			i.rect.x = i.rect.x - s
	def limitactiony(self,s):
		for m in self.mapobj:
			m.rect.y = m.rect.y - s
		for i in self.i.items:
			i.rect.y = i.rect.y - s